package src.main.java.org.netWorkProgram.TCP.demo4;
/*
* TCP连接——群聊
* */
import java.io.DataOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

public class Client {
    public static void main(String[] args) throws Exception {

        //1.创建socket对象，并同时请求服务端连接
        Socket socket = new Socket("127.0.0.1", 8888);

        //创建独立线程，负责随机从socket中接受服务端发送过来的消息
        new ClientReaderThread(socket).start();

        //2.从socket通信管道得到字节输出流，用来发送数据给服务端程序
        OutputStream os = socket.getOutputStream();
        //3.低级的字节输出流包装成数据输出流
        DataOutputStream dos = new DataOutputStream(os);

        Scanner scanner = new Scanner(System.in);
        //4.开始写数据
        while (true) {
            System.out.println("请说：");
            String msg = scanner.nextLine();
            if("exit".equals(msg)){
                System.out.println("欢迎下次再来~");
                dos.close();
                socket.close();
                break;
            }
            dos.writeUTF(msg);
            dos.flush();
        }

    }
}
